package com.conan.zone.rss.support.parser;

import java.io.FileNotFoundException;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.conan.zone.rss.model.RssItem;
import com.conan.zone.util.DateUtil;

/**
 * qq订阅分析器
 * @author Conan
 *
 */
public class QQRssParser extends AbstractParser{
	
	public static void main(String[] args) throws FileNotFoundException{
		QQRssParser p = new QQRssParser();
		p.parser("http://news.qq.com/newsgj/rss_newswj.xml");
	}

	@Override
	public void characters(char[] ch, int offset, int length) throws SAXException {
		if(item!=null){
			String value = new String(ch, offset, length);
			if(importedValueTags.get(0).intValue() == VALUE_STARTED){
				//System.out.println("[title]:"+value);
				item.setTitle(value);
				importedValueTags.put(0, VALUE_IMPORTED);
			}
			if(importedValueTags.get(1).intValue() == VALUE_STARTED){
				//System.out.println("[link]:"+value);
				item.setLink(value);
				importedValueTags.put(1, VALUE_IMPORTED);
			}
			if(importedValueTags.get(2).intValue() == VALUE_STARTED){
				//System.out.println("[author]:"+value);
				item.setAuthor(value);
				importedValueTags.put(2, VALUE_IMPORTED);
			}
			if(importedValueTags.get(3).intValue() == VALUE_STARTED){
				try {
					//System.out.println("[PubDate]:"+value);
					importedValueTags.put(3, VALUE_IMPORTED);
					item.setPubDate(DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss", value));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(importedValueTags.get(4).intValue() == VALUE_STARTED){
				//System.out.println("[comments]:"+value);
				item.setComments(value);
				importedValueTags.put(4, VALUE_IMPORTED);
			}
			if(importedValueTags.get(5).intValue() == VALUE_STARTED){
				//System.out.println("[desc]:"+value);
				item.setDescription(value);
				importedValueTags.put(5, VALUE_IMPORTED);
			}
			if(importedValueTags.get(0).intValue() == VALUE_IMPORTED 
					&& importedValueTags.get(1).intValue() == VALUE_IMPORTED 
					&& importedValueTags.get(6).intValue() != VALUE_IMPORTED ){
				item.setRssCode(DigestUtils.md5Hex(item.getTitle()+item.getLink()));
				importedValueTags.put(6, VALUE_IMPORTED);
			}
			
			boolean canImportToList = true;
			for(Entry<Integer,Integer> entry : importedValueTags.entrySet()){
				//System.out.println("entry.getValue()="+entry.getValue());
				if(entry.getValue() != VALUE_IMPORTED){
					canImportToList = false;
				}
			}
			if(canImportToList){
				//System.out.println("to list");
				list.add(item);
				resetInit();
			}
		}
		
	}

	@Override
	public void startElement(String url, String local, String qName, Attributes atts) throws SAXException {
		//System.out.println("startElement start.........................");
		if(StringUtils.equalsIgnoreCase(local, "item")){
			startItem = true;
			item = new RssItem();
		}
		
		if(startItem && StringUtils.equalsIgnoreCase(local, "title")){
			importedValueTags.put(0, VALUE_STARTED);
		}else if(startItem && StringUtils.equalsIgnoreCase(local, "link")){
			importedValueTags.put(1, VALUE_STARTED);
		}else if(startItem && StringUtils.equalsIgnoreCase(local, "author")){
			importedValueTags.put(2, VALUE_STARTED);
		}else if(startItem && StringUtils.equalsIgnoreCase(local, "category")){
			
		}else if(startItem && StringUtils.equalsIgnoreCase(local, "pubDate")){
			importedValueTags.put(3, VALUE_STARTED);
		}else if(startItem && StringUtils.equalsIgnoreCase(local, "comments")){
			importedValueTags.put(4, VALUE_STARTED);
		}else if(startItem && StringUtils.equalsIgnoreCase(local, "description")){
			importedValueTags.put(5, VALUE_STARTED);
		}
		
		//System.out.println("startElement end.........................");
	}
}
